import VxeColumn from './VxeColumn';
import toolButtons from './ToolButtons';
import tableAction from './TableAction';
import { VxeGridInstance, BasicTableProps } from '@/components/VxeTable';
import { ref, reactive } from 'vue';
import tableFilter from '@/components/TableFilter/index.vue';
import { UserParam } from '@/api/system/user/model';
export const TableRef = ref<VxeGridInstance>();
export const FilterRef = ref<InstanceType<typeof tableFilter>>();
export default {
  FilterRef: ref<InstanceType<typeof tableFilter>>()
};

export const useConfig = (queryCall: (param) => Promise<any>) => {
  const FilterRef = ref<InstanceType<typeof tableFilter>>();
  const TableRef = ref<VxeGridInstance>();
  // 默认搜索条件
  const defaultWhere = reactive<UserParam>({
    orgId: 0,
    userName: '',
    nickName: '',
    conModels: []
  });
  return {
    TableRef,
    FilterRef,
    defaultWhere,
    Columns: VxeColumn,
    ToolBtn: toolButtons,
    Action: tableAction,
    TableFilter: tableFilter,
    options: reactive<BasicTableProps>({
      showOverflow: '',
      border: true,
      height: 'auto',
      columnConfig: {
        resizable: true
      },
      columns: VxeColumn,
      proxyConfig: {
        ajax: {
          query: ({ page }) => {
            if (
              FilterRef.value &&
              FilterRef.value.conModelsJson &&
              FilterRef.value.conModelsJson.length
            ) {
              defaultWhere.conModels = FilterRef.value?.conModelsJson;
            }

            return queryCall({ ...page, ...defaultWhere });
          }
        }
      }
    })
  };
};
